package com.smalls.newvideotwo.mvp.model;

import android.os.Handler;

import com.smalls.newvideotwo.Constants;
import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.beanResult.GetHotMovieResult;
import com.smalls.newvideotwo.mvp.http.CommonParamsInterceptor;
import com.smalls.newvideotwo.mvp.http.Result;
import com.smalls.newvideotwo.mvp.model.modelInterface.IGetMovieByCategory;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.util.UIHelper;
import com.smalls.newvideotwo.utils.Constant;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by hs on 2018/3/20.
 */

public class GetMovieByCategoryModel implements IGetMovieByCategory {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetMovieByCategoryModel":"GetMovieByCategoryModel";
    Handler handler = new Handler();
    private Call call;
    public void Abort() {
        handler.removeCallbacksAndMessages(null);
        if(call!=null&&!call.isCanceled()) {
            call.cancel();
        }
    }
    @Override
    public void getMovie(final BaseBeanResult baseBeanResult,String category,int start,int end) {
        LogUtil.d(this.TAG, "getMovie category="+category+" start="+start+" end="+end);
        final Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json")
                .addHeader("Cache-Control", "no-cache")
                .addHeader("Range",start+"-"+end)
                .addHeader("Authorization","Bearer "+ NewDTVApp.getApp().getToken())
                .url(Constants.API_DETAIL())
                .get()
                .build();
        try {
        new OkHttpClient.Builder()
                .addInterceptor(NewDTVApp.getApp().loggingInterceptor)
                .addInterceptor(new CommonParamsInterceptor.Builder()
                        .addQueryParam("category_id", "eq."+category)
                        .build())
                .build()
                .newCall(request)
                .enqueue(new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, final IOException e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                baseBeanResult.onError(e);
                                //UIHelper.toastServerConnectErr();
                            }
                        });

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String resultStr = response.body().string();
                        if(response.code()!=200) {
                            baseBeanResult.onError(new Exception());
                            return;
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    LogUtil.d(TAG,"onResponse ");
                                    String addData = "{data:"+resultStr+"}";
                                    GetHotMovieResult result = Result.getResult(addData, GetHotMovieResult.class);
                                    if (result!=null) {
                                        baseBeanResult.onNext(result);
                                        return;
                                    }
                                    /// toastServerResponseErr(result.errorMessage);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    baseBeanResult.onError(e);
                                    UIHelper.toastServerResponseErr("something wrong with data.");
                                }
                            }
                        });

                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
        baseBeanResult.onError(e);
        //UIHelper.toastServerResponseErr("something wrong with data.");
    }
    }
}
